package db;

import java.sql.*;

import util.DBSettings;

public class DBConnection 
{
	private DatabaseMetaData dma;
	private static Connection con;

	private static DBConnection instance = null;

	private static boolean DEBUG = true;

	private DBConnection()
	{
		DBSettings dbSettings = new DBSettings();

		final String host = dbSettings.getDBSettingLine(1);
		final String port = dbSettings.getDBSettingLine(2);
		final String databaseName = dbSettings.getDBSettingLine(3);
		final String userName = dbSettings.getDBSettingLine(4);
		final String password = dbSettings.getDBSettingLine(5);

		String url = "jdbc:sqlserver://" + host + ":" + port + ";database=" + databaseName + ";user=" + userName + ";password=" + password;

		try 
		{
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

			if (DEBUG)
				System.out.println("[DBConnection] Loaded driver");
		}
		catch(Exception e) 
		{
			if (DEBUG)
			{
				System.out.println("[DBConnection] Can not find the driver");
				System.out.println(e.getMessage());
			}
		}

		try 
		{           
			con = DriverManager.getConnection(url);
			con.setAutoCommit(true);

			dma = con.getMetaData();

			if (DEBUG)
			{
				System.out.println("[DBConnection] Connection to: " + dma.getURL());
				System.out.println("[DBConnection] Driver: " + dma.getDriverName());
				System.out.println("[DBConnection] Database product name: " + dma.getDatabaseProductName());
			}
		}
		catch(Exception e) 
		{
			if (DEBUG)
			{
				System.out.println("[DBConnection] Problems with the connection to the database");
				System.out.println(e.getMessage());
				System.out.println(url);
			}
		}
	}

	public static void closeConnection()
	{
		try 
		{
			con.close();

			if (DEBUG)
				System.out.println("[DBConnection] The connection is closed");
		}
		catch (Exception e) {
			if (DEBUG)
				System.out.println("[DBConnection] Error trying to close the database: " +  e.getMessage());
		}
	}

	public  Connection getDBcon()
	{
		return con;
	}

	public static DBConnection getInstance()
	{
		if (instance == null)
			instance = new DBConnection();

		return instance;
	}

	public static void startTransaction()
	{ 
		try 
		{
			con.setAutoCommit(false);
		}
		catch(Exception e)
		{
			if (DEBUG)
			{
				System.out.println("[DBConnection] Error starting the transaction");
				System.out.println(e.getMessage());
			}
		}
	}

	public static boolean commitTransaction()
	{ 
		try 
		{
			con.commit();
			con.setAutoCommit(true);

			return true;
		}
		catch(SQLException e) 
		{
			if (DEBUG)
			{
				System.out.println("[DBConnection] Error committing the transaction");
				System.out.println(e.getMessage());
			}

			return false;
		}
	}  

	public static void rollbackTransaction()
	{ 
		try
		{
			con.rollback();
			con.setAutoCommit(true);
		}
		catch(Exception e) 
		{
			if (DEBUG)
			{
				System.out.println("[DBConnection] Error rollbacking the transaction");
				System.out.println(e.getMessage());
			}
		}
	}

	public static boolean doDebug()
	{
		return DEBUG;
	}

	public static void printDebug(String method, String query)
	{
		System.out.println("[" + method + "] " + query);
	}
}